package com.erclab.internal.xpresso.formelements;

import java.util.Properties;

import com.erclab.internal.xpresso.exceptions.FormException;
import com.erclab.internal.xpresso.formelements.FormElement;
import com.erclab.internal.xpresso.forms.Form;
import com.xpresso.utils.props.PropertyWorker;

public class MapLocalizer extends FormElement {

	@Override
	public boolean isModifiable() {
		return true;
	}

	@Override
	public void loadLookupValues(Form theForm) throws FormException {
		// Nothing needed.
	}

	@Override
	public String toHTML(Form theForm) {
		Properties props = new PropertyWorker().getProperties(this.parameters);
		StringBuffer sb = new StringBuffer();
		sb.append("<script src=\"" + props.getProperty("MapLocalizer.js.path") + "\"></script>");
		sb.append("<script>var mapLanguage = new Object();" +
					"mapLanguage.search='" + props.getProperty("MapLocalizer.lang.search") + "';" +
					"mapLanguage.reset='" + props.getProperty("MapLocalizer.lang.reset") + "';" +
					"mapLanguage.finish='" + props.getProperty("MapLocalizer.lang.finish") + "';" +
					"mapLanguage.pointmsg='" + props.getProperty("MapLocalizer.lang.pointmsg") + "';" +
					"mapLanguage.coordinates='" + props.getProperty("MapLocalizer.lang.coordinates") + "';" +
					"</script>");		
		sb.append("<INPUT TYPE=\"hidden\" name=\"Field");
		sb.append(this.idFormElement);
		sb.append("\"");
		sb.append(" id=\"MapField" + this.destinationColumn + "\" ");
		if (theForm.getFormParameters().getAction().equals("update")
				&& originalValue != null && !originalValue.equals("")){
			sb.append("value=\"" + originalValue + "\">");
			sb.append("<span class=\"MapFieldTextSpan\" id=\"MapFieldText" + this.destinationColumn + "\">");
			sb.append(props.getProperty("MapLocalizer.lang.localized"));
			sb.append("<a href=\"javascript:setCoordinates('" + this.destinationColumn + "')\">" + 
					props.getProperty("MapLocalizer.text.changeCoordinates") + "</a></span>");
		} else {		
			sb.append("><span class=\"MapFieldTextSpan\" id=\"MapFieldText" + this.destinationColumn + "\">" +
					"<a href=\"javascript:setCoordinates('" + this.destinationColumn + "')\">" + 
					props.getProperty("MapLocalizer.text.setCoordinates") + "</a></span>");
		}
		return sb.toString();
	}

}
